#!/bin/bash

cd ~/p2 

for folder in $(find * -type d)
do

rm -r ~/grade
unzip -d ~/grade/ ~/bruinbase.zip
cp ~/project2-test/*.del ~/grade/

cd ~/p2
cp ./$folder/* ~/grade/

cd ~/grade/
# start testing
make

if [ $? == 0 ]; then
  echo -e "Start Testing" > ~/done/$folder.txt
else
  echo -e "cant compile" > ~/done/$folder.txt 
  continue
fi

echo "LOAD xsmall FROM 'xsmall.del' WITH INDEX" > ~/tmp.sql

echo "load xsmall test" >> ~/done/$folder.txt 
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt  
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt 


echo "SELECT * FROM xsmall WHERE key <> 2342" > ~/tmp.sql

echo "case 1" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT * FROM xsmall WHERE key > 2000" > ~/tmp.sql

echo "case 2" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "LOAD small FROM 'small.del' WITH INDEX" > ~/tmp.sql

echo "load small test" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT * FROM small WHERE value = 'Doctor Zhivago'" > ~/tmp.sql

echo "case 3" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT value FROM small WHERE key < 50" > ~/tmp.sql

echo "case 4" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT * FROM small WHERE key = 1942 AND key > 1942" > ~/tmp.sql

echo "case 5" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT COUNT(*) FROM small WHERE key < 2400 AND key > 1600 AND key <= 50000" > ~/tmp.sql

echo "case 6" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "LOAD medium FROM 'medium.del' WITH INDEX" > ~/tmp.sql

echo "load medium test" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT COUNT(*) FROM medium WHERE key = 2619 AND key = 3351" > ~/tmp.sql

echo "case 7" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT value FROM medium WHERE key > 4000 AND value <> 'Wash, The'" > ~/tmp.sql

echo "case 8" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT * FROM medium WHERE key = 3936 AND value > 'Superman'" > ~/tmp.sql

echo "case 9" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt

echo "LOAD large FROM 'large.del' WITH INDEX" > ~/tmp.sql

echo "load large test" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt




echo "SELECT * FROM large WHERE key < 88 AND value > 'Ace'" > ~/tmp.sql

echo "case 10" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT * FROM large WHERE key <> 88 AND value = 'Alaska'" > ~/tmp.sql

echo "case 11" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT COUNT(*) FROM large WHERE key <> 4589" > ~/tmp.sql

echo "case 12" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT * FROM large WHERE key > 939 AND key <1000 AND key <= 940" > ~/tmp.sql

echo "case 13" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "LOAD xlarge FROM 'xlarge.del' WITH INDEX" > ~/tmp.sql

echo "load xlarge test" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT COUNT(*) FROM xlarge WHERE key < 3000" > ~/tmp.sql

echo "case 14" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT * FROM xlarge WHERE key > 40471000 AND value = 'Superman'" > ~/tmp.sql

echo "case 15" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT * FROM xlarge WHERE key < 500000000 AND key > 490000 AND key <=500000" > ~/tmp.sql

echo "case 16" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


rm -f xsmall.idx xsmall.tbl

echo "LOAD xsmall FROM 'xsmall.del'" > ~/tmp.sql
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt

echo "SELECT count(*) FROM xsmall WHERE key > 2000" > ~/tmp.sql

echo "case 17" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt

rm -f small.idx small.tbl
echo "LOAD small FROM 'small.del'" > ~/tmp.sql
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


echo "SELECT value FROM small WHERE key < 50" > ~/tmp.sql

echo "case 18" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
./bruinbase < ~/tmp.sql >> ~/done/$folder.txt 2>> ~/done/$folder.txt
echo "" >> ~/done/$folder.txt
date >> ~/done/$folder.txt
echo "=============" >> ~/done/$folder.txt


done;




